//一个最简单的promise示例
function timeout(ms){
	return new Promise((resolve,reject)=>{
		setTimeout(resolve,ms,'done');
	});
}
function doDone(name){
	console.log('is over');
	return new Promise((resolve,reject)=>{
		
	});
}
timeout(1000).then((value)=>{
	console.log(value);
});
doDone('你').then((value)=>{
	console.log(value);
});
//定义一个promiss对象
let promise=new Promise(function(resolve,reject){
	console.log('the first');
	resolve();
});
promise.then(function(){
	console.log('the second');
});
//异步加载图片的实例
console.log('Hi!');
//手动封装ajax请求，和jquery的封装是一致的
const getJSON=function(url){
	var promise=new Promise(function(resolve,reject){
		const handler=function(){
			if(this.readyState!==4){
				return;
			}
			if(this.status==200){
				resolve(this.response);
			}
			if(this){
				reject(new Error(this.statusText));
			}
		}
		const client=new XMLHttpRequest();
		client.open("Get",url);
		client.onreadystatechange=handler;
		client.responseType="json";
		client.setRequestHeader("Accept","application/json");
	});
	return promise;
}
getJSON('/post.json').then(function(json){
	console.log('contents'+json);
},function(error){
	console.error('出错了', error);
});
//两个promise的操作使用
var p1=new Promise(function(resolve,reject){
	console.log('首先操作的函数');
});
var p2=new Promise(function(resolve,reject){
	console.log('等会处理的函数');
	resolve(p1);//先执
});
//确定 promise实战应用中使用的模式
new Promise(function(resolve,reject){
	
	resolve(6);//等到最后执行
	console.log('第一步');
}).then((r)=>{
	console.log(r);
});
//resolve对应()=>{}，return对应function(value){}的value
//两个函数要按照顺序执行的例子
var myPromise=new Promise(function(resolve,reject){
	resolve(90);
});
myPromise.then(function(value){
	return value;
}).then(function(v){
	console.log(v);
});
//普通匿名函数写法的的catch 测试
var catchPromise=new Promise(function(resolve,reject){
	resolve();
});
catchPromise.then(function(){
	console.log(yue['ert']);
}).catch(function(err){
	console.log('代码异常');
});